package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.core.common.node.JsonResult;
import cn.stylefeng.guns.modular.api.model.*;
import cn.stylefeng.guns.modular.hp.constant.ArticleEnum;
import cn.stylefeng.guns.modular.hp.constant.MenuJumpTypeEnum;
import cn.stylefeng.guns.modular.hp.model.ArticleInfo;
import cn.stylefeng.guns.modular.hp.model.ExpertInfo;
import cn.stylefeng.guns.modular.hp.service.IArticleInfoService;
import cn.stylefeng.guns.modular.hp.service.IExpertInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/listPage")
@Api(tags = "1.0", description = "市中医院门户列表页", value = "门户列表页API")
public class ListPageApiController {

	private final static Logger log = LoggerFactory.getLogger(ListPageApiController.class);
	
	@Autowired
    private IArticleInfoService articleInfoService;

	@Autowired
	private IExpertInfoService expertInfoService;
	

	@SuppressWarnings("unchecked")
	@GetMapping("/getNoticePage")
    @ApiOperation(value = "获取通知公告分页")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageNo", value = "当前页数，以1为起始值", dataType = "Integer", paramType = "query")
	})
	public JsonResult<NoticeDTO> getNoticePage(@RequestParam Integer length, @RequestParam Integer pageNo) {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<ArticleInfo> infoList = this.articleInfoService.modularList(ArticleEnum.NOTICE, null);
		
		List<NoticeDTO> noticeDTOList = new ArrayList<NoticeDTO>();
		
		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				ArticleInfo info = infoList.get(i);
				noticeDTOList.add(new NoticeDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
			}
			//Collections.sort(noticeDTOList);
		}
		
		if(noticeDTOList != null && noticeDTOList.size() > length * (pageNo - 1)) {
			if(noticeDTOList.size() >= length * pageNo){
				noticeDTOList = noticeDTOList.subList(length * (pageNo - 1), length * pageNo);
			}
			else{
				noticeDTOList = noticeDTOList.subList(length * (pageNo - 1), noticeDTOList.size());
			}
		}else {
			noticeDTOList = new ArrayList<NoticeDTO>();
		}
		
		PageVo page = new PageVo(pageNo, length, infoList.size(), noticeDTOList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);
		
		return jr;
	}
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/getNewsPage")
    @ApiOperation(value = "获取新闻分页")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageNo", value = "当前页数，以1为起始值", dataType = "Integer", paramType = "query")
	})
	public JsonResult<NewsDTO> getNewsPage(@RequestParam Integer length, @RequestParam Integer pageNo) {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();
		
		List<ArticleInfo> infoList = this.articleInfoService.modularList(ArticleEnum.NEWS, null);
		
		List<NewsDTO> newsDTOList = new ArrayList<NewsDTO>();
		
		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				ArticleInfo info = infoList.get(i);
				newsDTOList.add(new NewsDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
			}
			//Collections.sort(noticeDTOList);
		}
		
		if(newsDTOList != null && newsDTOList.size() > length * (pageNo - 1)) {
			if(newsDTOList.size() >= length * pageNo){
				newsDTOList = newsDTOList.subList(length * (pageNo - 1), length * pageNo);
			}
			else{
				newsDTOList = newsDTOList.subList(length * (pageNo - 1), newsDTOList.size());
			}
		}else {
			newsDTOList = new ArrayList<NewsDTO>();
		}
		
		PageVo page = new PageVo(pageNo, length, infoList.size(), newsDTOList);
		
		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);
		
		return jr;
	}

	@SuppressWarnings("unchecked")
	@GetMapping("/getExpertPage")
	@ApiOperation(value = "获取专家介绍分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "当前页数，以1为起始值", dataType = "Integer", paramType = "query")
	})
	public JsonResult<ExpertDTO> getExpertPage(@RequestParam Integer length, @RequestParam Integer pageNo) {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();

		List<ExpertInfo> infoList = this.expertInfoService.expertList();

		List<ExpertDTO> expertDTOList = new ArrayList<ExpertDTO>();

		if(infoList != null && infoList.size() > 0) {
			for (int i = 0; i < infoList.size(); i++) {
				ExpertInfo info = infoList.get(i);
				expertDTOList.add(new ExpertDTO(info.getName(), info.getName(), info.getRank(), info.getPicUrl(), info.getPageContentId(), info.getPri(), MenuJumpTypeEnum.DETAILS));
			}
		}

		if(expertDTOList != null && expertDTOList.size() > length * (pageNo - 1)) {
			if(expertDTOList.size() >= length * pageNo){
				expertDTOList = expertDTOList.subList(length * (pageNo - 1), length * pageNo);
			}
			else{
				expertDTOList = expertDTOList.subList(length * (pageNo - 1), expertDTOList.size());
			}
		}else {
			expertDTOList = new ArrayList<ExpertDTO>();
		}

		PageVo page = new PageVo(pageNo, length, infoList.size(), expertDTOList);

		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(page);

		return jr;
	}

	@SuppressWarnings("unchecked")
	@GetMapping("/getAllDepartmentExpertList")
	@ApiOperation(value = "获取所有部门专家列表")
	public JsonResult<ExpertDTO> getAllDepartmentExpertList() {
		@SuppressWarnings("rawtypes")
		JsonResult jr = new JsonResult();

		List<ArticleInfo> departmentInfoList = this.articleInfoService.modularList(ArticleEnum.DEPARTMENT,null);

		List<DepartmentListDTO> departmentDTOList = new ArrayList<>();

		if(departmentInfoList != null && departmentInfoList.size() > 0) {
			for (int i = 0; i < departmentInfoList.size(); i++) {
				ArticleInfo info = departmentInfoList.get(i);
				List<ExpertInfo> expertList = this.expertInfoService.departmentExpertList(info.getId());

				List<ExpertDTO> expertDTOList = new ArrayList<>();
				for(ExpertInfo expertInfo : expertList){
					expertDTOList.add(new ExpertDTO(expertInfo.getName(), expertInfo.getName(), expertInfo.getRank(), expertInfo.getPicUrl(), expertInfo.getPageContentId(), expertInfo.getPri(), MenuJumpTypeEnum.DETAILS));
				}

				departmentDTOList.add(new DepartmentListDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), MenuJumpTypeEnum.DETAILS, expertDTOList));
			}
			Collections.sort(departmentDTOList);
		}

		jr.setResultCode(0);
		jr.setResultMsg("请求成功！");
		jr.setResultData(departmentDTOList);

		return jr;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@GetMapping("/getModelInfoPage")
    @ApiOperation(value = "获取各模块文章分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "modelName", value = "模块名称，可取值为：SRT-科研教学，HE-科普知识，KCM-对口帮扶，NG-护理园地，PBG-党建园地，MIP-医保政策，PA-药事管理，PR-政策法规", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageNo", value = "当前页数，以1为起始值", dataType = "Integer", paramType = "query")
	})
	public JsonResult<ModelDTO> getModelInfoPage(@RequestParam String modelName,@RequestParam Integer length, @RequestParam Integer pageNo) {
		JsonResult jr = new JsonResult();
		
		if(modelName != null) {
			modelName = modelName.toLowerCase();

			List<ArticleInfo> infoList = this.articleInfoService.modularList(modelName, null);
			
			List<ModelDTO> modelList = new ArrayList<ModelDTO>();
			
			if(infoList != null && infoList.size() > 0) {
				for (int i = 0; i < infoList.size(); i++) {
					ArticleInfo info = infoList.get(i);
					modelList.add(new ModelDTO(info.getTitle(), info.getLinkUrl(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
				}
				//Collections.sort(noticeDTOList);
			}
			
			if(modelList != null && modelList.size() > length * (pageNo - 1)) {
				if(modelList.size() >= length * pageNo){
					modelList = modelList.subList(length * (pageNo - 1), length * pageNo);
				}
				else{
					modelList = modelList.subList(length * (pageNo - 1), modelList.size());
				}
			}else {
				modelList = new ArrayList<ModelDTO>();
			}
			
			PageVo page = new PageVo(pageNo, length, infoList.size(), modelList);
			
			jr.setResultCode(0);
			jr.setResultMsg("请求成功！");
			jr.setResultData(page);
		}else {
			jr.setResultCode(1);
			jr.setResultMsg("请求失败，不存在的模块名！");
			jr.setResultData(null);
		}
		
		return jr;
	}
}
